/**
 * 
 */
package edu.uwm.nlp.jude.internal;

import java.util.ArrayList;

import edu.uwm.nlp.jude.util.NumberUtil;
import edu.uwm.nlp.jude.util.StringUtil;

/**
 * @author qing
 *
 * Jun 15, 2009
 */
public class FullReferences {
	
	private String fullRef;
	private String[] idsOfRefInt;
	
	public FullReferences(String fullRef)
	{
		this.fullRef = fullRef;
	}
	
	/**
	 * split reference with pattern [1] or (1)
	 * @param leftCoat [ or (
	 * @param rightCoat ] or )
	 * @return reference list of the article
	 */
	public ArrayList<ReferenceEntity> forkBrankedInt(char leftCoat, char rightCoat)
	{
		int curIdx = fullRef.indexOf(leftCoat);
		int nextIdx = curIdx;
		int startIdx;
		
		ReferenceEntity entity;
		ArrayList<ReferenceEntity> rEntList = new ArrayList<ReferenceEntity>();
		
		int i = 0;
		String idStr;
		String refStr;
		
		while(curIdx != -1)
		{
			nextIdx = fullRef.indexOf(rightCoat,curIdx);
			
			if(nextIdx == -1)
			{
				break;
			}
			
			startIdx = nextIdx +1;
			idStr = fullRef.substring(curIdx+1, nextIdx);
			
			
			do
			{
				curIdx = fullRef.indexOf(leftCoat,nextIdx);
				if(curIdx != -1)
					nextIdx = fullRef.indexOf(rightCoat,curIdx);
			}
			while(curIdx !=-1 && (fullRef.charAt(curIdx-1)!='\n'));
			
			if((curIdx >= 2) && (startIdx < curIdx -2))
				refStr = fullRef.substring(startIdx,curIdx-2);
			else
				refStr = fullRef.substring(startIdx);
			
			if(NumberUtil.isNum(idStr))
			{
				entity = new ReferenceEntity(idStr.trim(),refStr);
//				System.out.println(idStr+"=="+refStr);
				rEntList.add(entity);
				i++;
			}
			else
			{
				System.err.println("Not a ref index: "+idStr);
			}
				
		}
		return rEntList;
		
	}
	
	public ArrayList<ReferenceEntity> forkBrankedAuthor(char leftCoat, char rightCoat)
	{
		int curIdx = fullRef.indexOf(leftCoat);
		int nextIdx = curIdx;
		int startIdx;
		
		ReferenceEntity entity;
		ArrayList<ReferenceEntity> rEntList = new ArrayList<ReferenceEntity>();
		
		int i = 0;
		String idStr;
		String refStr;
		
		while(curIdx != -1)
		{
			nextIdx = fullRef.indexOf(rightCoat,curIdx);
			
			if(nextIdx == -1)
			{
				break;
			}
			
			startIdx = nextIdx +1;
			idStr = fullRef.substring(curIdx+1, nextIdx);
			
			
			do
			{
				curIdx = fullRef.indexOf(leftCoat,nextIdx);
				if(curIdx != -1)
					nextIdx = fullRef.indexOf(rightCoat,curIdx);
			}
			while(curIdx !=-1 && (fullRef.charAt(curIdx-1)!='\n'));
			
			if(curIdx != -1)
				refStr = fullRef.substring(startIdx,curIdx-2);
			else
				refStr = fullRef.substring(startIdx);
			
			if(idStr.matches("[A-Z][a-zA-Z\\s]+[0-9]*"))
			{
				entity = new ReferenceEntity(idStr.trim(),refStr);
//				idsOfRefInt[i] = idStr; // Modified on Mar 23, 2010
//				System.out.println(idStr+"=="+refStr);
				rEntList.add(entity);
				i++;
			}
			else
			{
				System.err.println("Not a ref index: "+idStr);
			}
				
		}
		return rEntList;
	}
	
	public ArrayList<ReferenceEntity> forkIntSimple(char seperator)
	{
		int curIdx = fullRef.indexOf('\n');
		curIdx = StringUtil.skipEscps(fullRef, curIdx);
		int nextIdx = curIdx;
		int startIdx;
		int num;
		String nextNumStr;
		String nextNumStr2;
		ReferenceEntity entity;
		ArrayList<ReferenceEntity> rEntList = new ArrayList<ReferenceEntity>();
		String idStr;
		String refStr;
		int i=0;
		while(curIdx !=-1 && nextIdx != -1)
		{
			nextIdx = fullRef.indexOf(seperator,curIdx+1);
			
			if(nextIdx == -1)break;
						// the scan reach the end
			startIdx = nextIdx+1;
			idStr = fullRef.subSequence(curIdx+1, nextIdx).toString();
			if(!NumberUtil.isNum(idStr)) // not a valid number, not an id, skip
			{
				curIdx = nextIdx+1;
				continue;
			}
			num = Integer.parseInt(idStr);
			nextNumStr = (new Integer(num+1)).toString();
			nextNumStr2 = (new Integer(num+2)).toString();
			
			do
			{
				curIdx = fullRef.indexOf('\n', nextIdx);
				if(curIdx !=-1) // low probability that curIdx = -1, because there is always an end in the article
				{
					nextIdx = fullRef.indexOf(seperator, curIdx);
					if(nextIdx != -1)
						curIdx = fullRef.lastIndexOf('\n', nextIdx);
				}
			}
			while(nextIdx != -1 && curIdx!=-1 && 
					!(fullRef.subSequence(curIdx+1, nextIdx).equals(nextNumStr)||fullRef.subSequence(curIdx+1, nextIdx).equals(nextNumStr2)));
								// some times the editor makes mistake and the nextNum is not continues
								// this case is ignored in this version
			if(nextIdx == -1)
				refStr = fullRef.substring(startIdx);
			else
				refStr = fullRef.substring(startIdx, curIdx); //BE CAREFUL, MAYBE NEED MORE CHECK!
			entity = new ReferenceEntity(idStr,refStr);
//			System.out.println(idStr+"=="+refStr);
			rEntList.add(entity);
//			idsOfRefInt[i] = idStr;
			i++;
		}
		return rEntList;
	}
	
	public ArrayList<ReferenceEntity> forkIntHalfPranph(char seperator)
	{
		int curIdx = fullRef.indexOf('\n');
		curIdx = StringUtil.skipEscps(fullRef, curIdx);
		int nextIdx = curIdx;
		int startIdx;
		int num;
		String nextNumStr;
		String nextNumStr2;
		ReferenceEntity entity;
		ArrayList<ReferenceEntity> rEntList = new ArrayList<ReferenceEntity>();
		String idStr;
		String refStr;
		int i=0;
		while(curIdx !=-1 && nextIdx != -1)
		{
			nextIdx = fullRef.indexOf(seperator,curIdx+1);
			
			if(nextIdx == -1)break;
						// the scan reach the end
			startIdx = nextIdx+1;
			idStr = fullRef.subSequence(curIdx+1, nextIdx).toString();
			if(!(NumberUtil.isNum(idStr) && StringUtil.isInHalfPranph(fullRef, nextIdx-1))) // not a valid number, not an id, skip
			{
				curIdx = nextIdx+1;
				continue;
			}
			num = Integer.parseInt(idStr);
			nextNumStr = (new Integer(num+1)).toString();
			nextNumStr2 = (new Integer(num+2)).toString();
			
			do
			{
				curIdx = fullRef.indexOf('\n', nextIdx);
				if(curIdx !=-1) // low probability that curIdx = -1, because there is always an end in the article
				{
					nextIdx = fullRef.indexOf(seperator, curIdx);
					if(nextIdx != -1)
						curIdx = fullRef.lastIndexOf('\n', nextIdx);
				}
			}
			while(nextIdx != -1 && curIdx!=-1 && 
					!(fullRef.subSequence(curIdx+1, nextIdx).equals(nextNumStr)||fullRef.subSequence(curIdx+1, nextIdx).equals(nextNumStr2)));
								// some times the editor makes mistake and the nextNum is not continues
								// this case is ignored in this version
			if(nextIdx == -1)
				refStr = fullRef.substring(startIdx);
			else
				refStr = fullRef.substring(startIdx, curIdx); //BE CAREFUL, MAYBE NEED MORE CHECK!
			entity = new ReferenceEntity(idStr,refStr);
//			System.out.println(idStr+"=="+refStr);
			rEntList.add(entity);
//			idsOfRefInt[i] = idStr;
			i++;
		}
		return rEntList;
	}
	
	public ArrayList<ReferenceEntity> forkAuthor()
	{
		StringIDAnalyzer strAnl = new StringIDAnalyzer(fullRef);
		return strAnl.forkStringId();
	}

}
